package com.znsd.zl.app.user.exception;

import com.znsd.zl.exception.RRException;
import com.znsd.zl.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * <p>
 *     全局异常处理器
 * </p>
 * @ClassName AppUserEntity
 * @Description 全局异常处理器
 * @author jaypdd
 * @email jaypdd@163.com
 * @date 2020-08-27 17:26:01
 * @Version v1.0
 */
@RestControllerAdvice
@Slf4j
public class MyExceptionHandler {

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(RRException.class)
	public R myExceptionHandler(RRException e){
		R r = new R();
		r.put("code", e.getCode());
		r.put("msg", e.getMessage());

		return r;
	}

	/**
	 * 全局错误异常信息封装
	 * @return 返回错误信息，可自定义异常信息
	 */
	@ExceptionHandler(Exception.class)
	public R handleException(Exception e){
		log.error(e.getMessage(), e);
		return R.error();
	}
}
